package com.example.springboot.controller;

import com.example.springboot.pojo.*;
import com.example.springboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private StockService stockService;
    @Autowired
    private TradeService tradeService;
    @Autowired
    private RenoteService renoteService;
//    @RequestMapping("/info")
//    public String index(Model model) {
//        List<BookInfo> bookInfos = bookService.getBookInfo();
//        model.addAttribute("msg", bookInfos);
//        return "书籍信息";
//    }

    @RequestMapping("/info")
    public String index3(Integer pageNumber, Model model) {
        int spPage = 0;
        int pageSize = 10;
        int pageNo = 1;
//        if (spPage == null) {
//            pageNo = 1;
//        } else {
//            pageNo = Integer.parseInt(spPage);
//            if (pageNo < 1) {
//                pageNo = 1;
//            }
//        }
        if(pageNumber == null || pageNumber <= 0){
            spPage = 1;
        }
        else{
            spPage = pageNumber;
        }
        int totalCount = 0;
        int count = bookService.selectService();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        List<BookInfo> list = bookService.pagelist(spPage - 1, pageSize);
        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        model.addAttribute("stpage", spPage);
        return "书籍信息";
    }

//    @RequestMapping("/dd")
//    public String index1(Model model) {
//        List<trade> tradeList = tradeService.getTrade();
//        model.addAttribute("msg", tradeList);
//        return "订单";
//    }

    @RequestMapping("/search")
    public String search(String info, Model model) {
        List<BookInfo> bookInfo = bookService.search(info);
        model.addAttribute("msg", bookInfo);
        return "查询";
    }

    @RequestMapping("/tjts")
    public String tjts() {
        return "添加图书";
    }

    @RequestMapping("/tjcg")
    public String tjcg(String text1, String text2, String text3, String text4, String text5, String text6, String text7, String text8, Model model) {
        int x = Integer.parseInt(text1);
        int y = Integer.parseInt(text5);
        double z = Double.parseDouble(text4);
        double k = Double.parseDouble(text8);
        BookInfo bookInfo = bookService.getBook(x);
        int num1 = 0;
        String s = null;
        if (bookInfo != null) {
            int m = y + bookInfo.getSum();
            num1 = bookService.upadteBook(new BookInfo(x, text2, text7, text6, m, k));
        } else {
            num1 = bookService.addBook(new BookInfo(x, text2, text7, text6, y, k));
        }
        int n = stockService.sumBook();
        int num2 = stockService.addStock(new Stock(n + 1, x, text2, text3, z, y));
        if (num1 != 0 && num2 != 0) {
            s = "添加成功";
        } else {
            s = "添加失败";
        }
        model.addAttribute("msg1", s);
        model.addAttribute("msg2", text2);
        model.addAttribute("msg3", text3);
        model.addAttribute("msg4", text4);
        model.addAttribute("msg5", text5);
        return "进货单";
    }

    @RequestMapping("/th")
    public String th() {
        return "退货";
    }

    @RequestMapping("/gmts")
    public String gmts(String text1, Model model) {
        model.addAttribute("mag",text1);
        return "购买图书";
    }

    @RequestMapping("/buy")
    public String buy(String text1, String text3, Model model) {
        int x = Integer.parseInt(text1);
        int y = Integer.parseInt(text3);
        BookInfo bookInfo = bookService.getBook(x);
        int a = bookInfo.getSum();
        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String s = simpleDateFormat.format(date);
        double sum = y * bookInfo.getPrice();
        int m = tradeService.sumTrade();
        int n = tradeService.addTrade(new trade(m + 1, date, sum, y));
        int b = bookService.upadteBook(new BookInfo(bookInfo.getId(), bookInfo.getBookname(), bookInfo.getWriter(), bookInfo.getDt(), a - y, bookInfo.getPrice()));
        model.addAttribute("msg2", bookInfo.getBookname());
        model.addAttribute("msg3", bookInfo.getWriter());
        model.addAttribute("msg4", y);
        model.addAttribute("msg5", sum);
        return "销售单";
    }

    @RequestMapping("/tuihuo")
    public String tuihuo(String text1, String text2, String text3, String text4, Model model) {
        int n = renoteService.sumRenote();
        int a = -Integer.parseInt(text1);
        int m = Integer.parseInt(text2);
        int b = tradeService.sumTrade();
        Date date = new Date();
        BookInfo bookInfo = bookService.getBook(m);
        double sum = a * bookInfo.getPrice();
        renoteService.addRenote(new renote(n + 1, m, text3, text4));
        tradeService.addTrade(new trade(b + 1, date, sum, a));
        bookService.upadteBook(new BookInfo(bookInfo.getId(), bookInfo.getBookname(), bookInfo.getWriter(), bookInfo.getDt(), bookInfo.getSum() - a, bookInfo.getPrice()));
        model.addAttribute("msg2", text2);
        model.addAttribute("msg3", text3);
        model.addAttribute("msg4", text4);
        return "退货单";
    }

    @RequestMapping("/rxsb")
    public String rxsb(Model model) {
        List<trade> days = tradeService.countxse();
        model.addAttribute("msg", days);
        return "日销售表";
    }
    @RequestMapping("/yxsb")
    public String yxsb(Model model) {
        List<trade> months = tradeService.countyxse();
        model.addAttribute("msg", months);
        return "月销售表";
    }
    @RequestMapping("/phb")
    public String yxsbphb(Model model) {
        List<trade> months = tradeService.countyxsephb();
        model.addAttribute("msg", months);
        return "月销售表";
    }
}
